package com.fay.designPattern.principles.solid;

import com.google.gson.Gson;

import java.util.Collections;
import java.util.Map;

/**
 * @Title:
 * @Description: 协议格式，字符串，gson
 * @Author Fay Wang
 * @Date 2022/3/1 15:06
 * @Version 1.0
 */
public class SrpSerialization {
    private static final String IDENTIFIER_STRING = "UEUEUE;";
    private Gson gson;

    public SrpSerialization(Gson gson) {
        this.gson = gson;
    }

    public String serialize(Map<String, String> object) {
        StringBuilder textBuilder = new StringBuilder();
        textBuilder.append(IDENTIFIER_STRING);
        textBuilder.append(gson.toJson(object));
        return textBuilder.toString();
    }

    public Map<String, String> deserialize(String text) {
        if (!text.startsWith(IDENTIFIER_STRING)) {
            return Collections.emptyMap();
        }
        String gsonStr = text.substring(IDENTIFIER_STRING.length());
        return gson.fromJson(gsonStr, Map.class);
    }
}
